//
//  RecommendOfCell.m
//  MY Project For Sport
//
//  Created by dllo on 16/1/26.
//  Copyright © 2016年 dilo. All rights reserved.
//

#import "RecommendOfCell.h"
#import "CellForRecommedImage.h"
#import "NetworkHandle.h"
#import "ModelForRecommend.h"
#import "CellForRecommendNews.h"
#import <MJRefresh.h>
#import <DKNightVersion.h>
#import "UIColor+Wonderful.h"
#define WEIGHTT [UIScreen mainScreen].bounds.size.width / 375
#define HEIGHTT [UIScreen mainScreen].bounds.size.height / 667



@interface RecommendOfCell ()<UITableViewDataSource, UITableViewDelegate, NetworkHandleDelegate>

@property (nonatomic, retain) UISearchBar *bar;
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *array;
@property (nonatomic, retain) NSString *strUrl;
@property (nonatomic ,assign) NSInteger  offset;

@end

@implementation RecommendOfCell

-(void)dealloc {

    [_bar release];
    [_tableView release];
    [_array release];
    [_strUrl release];
    [super dealloc];

}

-(instancetype)initWithFrame:(CGRect)frame {

    self = [super initWithFrame:frame];
    
    if (self) {
        
        [self createSubview];
        [self handleData];
        
        self.offset = 0;
        [self createFootRefresh];
        [self handleDataUpData];
        
    }

    return self;

}

- (void)createSubview {

    
    
    
    self.bar = [[UISearchBar alloc] init];
    [self.contentView addSubview:self.bar];
    [_bar release];
 
    self.bar.placeholder = @"搜索";
    
    
    if (self.array.count == 0) {
        self.array = [NSMutableArray array];
        
    }

    self.tableView = [[UITableView alloc] init];
    [self.contentView addSubview:self.tableView];
    [_tableView release];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.tableView.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor], [UIColor grayColor]);
    
    self.tableView.dataSource = self;
    self.tableView.delegate = self;

    
    [self.tableView registerClass:[CellForRecommedImage class] forCellReuseIdentifier:@"poolForRecommendImage"];
    [self.tableView registerClass:[CellForRecommendNews class] forCellReuseIdentifier:@"poolForRecommendNews"];
    
}

/** 下拉加载 */
- (void)handleDataUpData {
    
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        self.array = [NSMutableArray array];
        
        self.offset = 0;
        
        [self handleData];
        
        
    }];
    
    self.tableView.mj_header = header;
    
}


/**数据处理 */
- (void)handleData {

   // NSString *str = @"http://platform.sina.com.cn/sports_client/cre?app_key=2586208540&_version__=3.2.1.01&__os__=android&f=title%2Curl%2Ccategoryid%2Cimg%2Cimages%2Ccomment_total%2Ccomment_show%2Cctime%2Cvid%2Cvideo_info%2Cstitle%2Cwapsummary&length=20&client_deviceid=7005145ee5bbe7f1f97c5e227f4ab771";
    
    
    
    NSString *str = @"http://platform.sina.com.cn/sports_client/cre?app_key=2586208540&_version__=3.2.1.01&__os__=android&f=title%2Curl%2Ccategoryid%2Cimg%2Cimages%2Ccomment_total%2Ccomment_show%2Cctime%2Cvid%2Cvideo_info%2Cstitle%2Cwapsummary&length=20&client_deviceid=e1626cc2f63146d9302c32f415dfedf0";
    

    [NetworkHandle handlerJSONWitURL:str delegate:self];
    
}

/**上拉刷新*/
- (void)createFootRefresh {
 
    
    MJRefreshAutoNormalFooter *foot = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
       
        NSString *str = @"http://platform.sina.com.cn/sports_client/cre?app_key=2586208540&_version__=3.2.1.01&__os__=android&f=title%2Curl%2Ccategoryid%2Cimg%2Cimages%2Ccomment_total%2Ccomment_show%2Cctime%2Cvid%2Cvideo_info%2Cstitle%2Cwapsummary&offset=";
        
        NSString *str2 = @"&length=20&client_deviceid=e1626cc2f63146d9302c32f415dfedf0";
        //NSString *str2 =@"&length=20&client_deviceid=7005145ee5bbe7f1f97c5e227f4ab771";
        self.offset = self.offset + 20;
        self.strUrl = [str stringByAppendingFormat:@"%ld%@", self.offset, str2];
        
        [self handleData1];
        
        
    }];
    
    self.tableView.mj_footer = foot;


}

- (void)handleData1 {
    
    
    [NetworkHandle handlerJSONWitURL:self.strUrl delegate:self];

}

- (void)handleDidComplete:(id)result {
    
 
    NSDictionary *dic = [result objectForKey:@"result"];
    
    NSDictionary *dict = [dic objectForKey:@"data"];
    
    NSDictionary *dics = [dict objectForKey:@"feed"];
    
    NSArray *array = [dics objectForKey:@"data"];
    
    for (NSDictionary *dic in array) {
    
        ModelForRecommend *model = [[ModelForRecommend alloc] init];
        
        [model setValuesForKeysWithDictionary:dic];
        
        [self.array addObject:model];
        [model release];
        
    }
    
    [self.tableView reloadData];
    [self.tableView.mj_footer endRefreshing];
    [self.tableView.mj_header endRefreshing];
    

}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.array.count ;

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    ModelForRecommend *model = [self.array objectAtIndex:indexPath.row];
    
    if ([model.wapsummary isEqualToString:@""]&& [model.categoryid isEqualToString:@"2"]) {
        
        CellForRecommedImage *cell = [tableView dequeueReusableCellWithIdentifier:@"poolForRecommendImage"];
        [cell passModelForRecommedImage:model];
        return cell;
        
    }else{
    
        CellForRecommendNews *cell = [tableView dequeueReusableCellWithIdentifier:@"poolForRecommendNews"];
        
        [cell passModelForRecommend:model];
        

    return cell;
        
    }

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    ModelForRecommend *model = [self.array objectAtIndex:indexPath.row];

    if ([model.wapsummary isEqualToString:@""] && [model.categoryid isEqualToString:@"2"]) {
        
        return tableView.frame.size.height / 4.5;
        
    }else {
    
        return tableView.frame.size.height / 5.5;
    
    }

}



- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes {
    
    self.bar.frame = CGRectMake(0, 0, self.contentView.frame.size.width, self.contentView.frame.size.height / 15);
    
    self.tableView.frame = CGRectMake(0, self.contentView.frame.size.height / 15, self.contentView.frame.size.width, self.contentView.frame.size.height - self.contentView.frame.size.height / 15);
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"disappear" object:nil];

    [[NSNotificationCenter defaultCenter] postNotificationName:@"id" object:nil userInfo:@{@"ID":[self.array objectAtIndex:indexPath.row]}];
 }


@end
